﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Uniforax.Camp.TestData.Common;
using Uniforax.Core.Common;
using Uniforax.Core.GameObjects;

namespace Uniforax.Camp.TestData.SmallStake
{
    public class SmallStake : BaseGameObject
    {
        public SmallStakeSkillManager SmallStakeSkillManager => _skillManager;
        private SmallStakeSkillManager _skillManager;

        public SmallStake() : base()
        {
            Init();
        }

        public SmallStake(SmallStake copy) : base(copy)
        {
            Copy(copy);
        }

        private void Init()
        {
            HP = 100;
            PowerRequired = 1;
            Movement = new SmallStakeMovement();
            _skillManager = new SmallStakeSkillManager();
            SkillManager = _skillManager;
            ArmorManager = new TestDataArmorManager();
        }

        private void Copy(SmallStake copy)
        {
            HP = copy.HP;
            PowerRequired = copy.PowerRequired;
            Movement = copy.Movement.Clone() as SmallStakeMovement;
            _skillManager = copy.SkillManager.Clone() as SmallStakeSkillManager;
            SkillManager = _skillManager;
            ArmorManager = copy.ArmorManager.Clone() as TestDataArmorManager;
        }

        public override object Clone()
        {
            return new SmallStake(this);
        }

        public override void OnCreate(ActionContext context)
        {
            base.OnCreate(context);
        }

        public override void OnPlacedGrid(ActionContext context, GameObjectPlaceGridMovementContext moveContext)
        {
            UnlockAttackUpAuto(context);

            TestDataArmorManager.HandleOnPlaceUnlockUpgradeArmorAuto(context, this);

            base.OnPlacedGrid(context, moveContext);
        }

        private void UnlockAttackUpAuto(ActionContext context)
        {
            // 新对象创建并落地，
            // 会检查技能解锁条件自行解锁新的技能。
            if (CreateStatus != CreateStatus.Completed)
            {
                var world = context.GameWorld;
                var playerData = world.GetPlayerData(context.PlayerId);

                var unlockAttackUpSkillId = Shared.UnlockPowerUpSkillId;

                var hasUnlockedAttackUp = playerData.GetField(unlockAttackUpSkillId);

                // 执行升级。
                if (hasUnlockedAttackUp)
                {
                    var upgradeAction = new UpgradeSmallStakeAction(this);
                    context.History.Add(upgradeAction);
                    upgradeAction.Execute(context);
                }
            }
        }

    }
}
